﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Xml;

using umbraco.BusinessLogic.Actions;
using umbraco.cms.presentation.Trees;
using umbraco.interfaces;

namespace CodeGecko.Modules.Umbraco.AppCodeEditor
{
	public class UserControlsTree : FileSystemTree
	{
		public UserControlsTree(String application) : base(application) { }

		protected override void CreateAllowedActions(ref List<IAction> actions)
		{
			actions.Clear();
			actions.Add(ActionRefresh.Instance);
		}

		protected override void CreateRootNodeActions(ref List<IAction> actions)
		{
			actions.Clear();
			actions.Add(ActionRefresh.Instance);
		}

		protected override void CreateRootNode(ref XmlTreeNode rootNode)
		{
			rootNode.Text = "User Controls";
			rootNode.Icon = FolderIcon;
			rootNode.OpenIcon = FolderIconOpen;
			rootNode.NodeID = "init";
			rootNode.NodeType = String.Concat(rootNode.NodeID, this.TreeAlias);
		}

		protected override string FilePath
		{
			get { return String.Concat(umbraco.GlobalSettings.Path, "/../usercontrols/"); }
		}

		protected override string FileSearchPattern
		{
			get { return "*.ascx"; }
		}

		protected override void OnRenderFileNode(ref XmlTreeNode xNode)
		{
			xNode.Action = xNode.Action.Replace("openFile", "openUserControlEditor");
			xNode.Icon = "settingXML.gif";
			xNode.OpenIcon = "settingXML.gif";
		}

		protected override void OnRenderFolderNode(ref XmlTreeNode xNode)
		{
			xNode.Menu = new List<IAction>(new IAction[] { ActionRefresh.Instance });
			xNode.NodeType = "userControlsFolder";
		}

		public override void RenderJS(ref StringBuilder Javascript)
		{
			Javascript.Append(@"
				function openUserControlEditor(file) {
					parent.right.document.location.href = 'developer/editCodeFile.aspx?file=/usercontrols/' + file;
				}
			");
		}
	}
}
